package com.lujianfei.waterpower.ui.water;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import com.lujianfei.share.ShareHelper;
import com.lujianfei.share.shareBase.SharePlatforms;
import com.lujianfei.waterpower.ui.water.waterAdd.WaterAddActivity;

import java.util.List;

import androidx.appcompat.app.AlertDialog;

public class WaterPresenter implements WaterContract.Presenter {

    WaterContract.View view;
    WaterContract.Model model;

    public WaterPresenter(WaterContract.View view) {
        this.view = view;
        model = new WaterModel();
    }


    @Override
    public void loadData() {
        List<WaterEntity> powerEntities = model.getData();
        view.updateData(powerEntities);
    }

    @Override
    public void openPowerAdd() {
        view.getContext().startActivity(new Intent(view.getContext(),WaterAddActivity.class));
    }

    @Override
    public void showOperDialog(final WaterEntity powerEntity) {
        CharSequence[] items = null;
        if (null != powerEntity.getNext()){
            items = new CharSequence[]{
                    "编辑", "删除","分享","取消"
            };
        }else{
            items = new CharSequence[]{
                    "编辑", "删除","取消"
            };
        }
        final CharSequence[] finalItems = items;
        new AlertDialog.Builder(view.getContext()).setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                CharSequence item = finalItems[which];
                if ("编辑".equalsIgnoreCase(""+item)){
                    openEdit(powerEntity);
                }else if ("删除".equalsIgnoreCase(""+item)){
                    confirmDialog(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            delete(powerEntity);
                            loadData();
                        }
                    });
                }else if ("分享".equalsIgnoreCase(""+item)){
                    String content = model.getShareContent(view.getContext(),powerEntity);
                    String title = "标题";
                    String dialogTitle = "选择分享的平台";
                    new ShareHelper(view.getContext())
                            .share(title,content,dialogTitle)
                            .withPlatforms(
                                    SharePlatforms.ShareTypeWechat,
                                    SharePlatforms.ShareTypeQQ)
                            .show();
                }
                dialog.dismiss();
            }
        }).setTitle("操作").show();
    }

    private void confirmDialog(final View.OnClickListener onOkClick) {
        new AlertDialog.Builder(view.getContext()).setTitle("提示").setMessage("确认删除？")
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (null!=onOkClick){
                            onOkClick.onClick(null);
                        }
                    }
                }).setNegativeButton("取消",null).show();
    }

    void openEdit(WaterEntity powerEntity){
        Intent intent = new Intent(view.getContext(), WaterAddActivity.class);
        intent.putExtra(WaterAddActivity.KEY_ID,powerEntity.getId());
        view.getContext().startActivity(intent);
    }

    void delete(WaterEntity powerEntity){
        powerEntity.delete();
    }
}
